﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AteneoLibri.DomainObject;
using AteneoLibriBusinessLogic.Entities;

namespace AteneoLibri.BackEnd
{
    public partial class InsertCity : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    List<AteneoLibriBusinessLogic.Entities.City> cities = service.getCities();

                    List<CityInfo> cityList = new List<CityInfo>();
                    foreach (var item in cities)
                    {
                        CityInfo cityInfo = new CityInfo();
                        cityInfo.IdCity = item.IdCity;
                        cityInfo.Description = item.Description;
                        cityInfo.Nation = service.getNation(item.NationId.Value).Description;

                        cityList.Add(cityInfo);
                    }

                    gvCities.DataSource = cityList.OrderBy(x => x.Nation).ThenBy(x => x.Description);
                    gvCities.DataBind();

                    ddlNations.DataSource = service.getNations().ToList();
                    ddlNations.DataBind();
                }
            }
        }

        //protected void gvCities_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    gvCities.PageIndex = e.NewPageIndex;
        //    gvCities.DataBind();
        //}

        protected void btnInsertCity_Click(object sender, EventArgs e)
        {
            string cityName = txtCity.Text.Trim();
            if (cityName != null && cityName != "")
            {
                try
                {
                    City city = new City();
                    using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                    {
                        city.IdCity = Guid.NewGuid();
                        city.Description = cityName;
                        city.NationId = new Guid(ddlNations.SelectedItem.Value);

                        bool result = service.insertCity(city);
                        if (result)
                        {
                            // open panel

                            // refresh page
                            Response.Redirect("InsertCity.aspx");
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void btnDeleteCity_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = sender as ImageButton;
            string idCity = button.CommandArgument;

            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                Guid cityId = new Guid(idCity);
                City city = service.getCity(cityId);

                List<University> universities = service.getUniversities().Where(x => x.CityId == cityId).ToList();

                if (universities.Count() == 0)
                {
                    var result = service.deleteCity(city);
                    if (result)
                    {
                        // refresh page
                        Response.Redirect("InsertCity.aspx");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Attenzione!", "alert('Cancellare prima tutte le università della città prima di procedere alla rimozione della città.');", true);
                }



            }

        }

    }
}